// ==UserScript==
// @name         Cookies获取器
// @namespace    https://www.52pojie.cn/forum.php?mod=viewthread&tid=2050064
// @version      2.0
// @description  获取并复制当前页面的Cookies，带后台开关控制
// @author       52pojie baicj
// @match        *://*/*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// @icon         https://gitee.com/selling-surprises/Greasyfork-Script-repository/raw/main/%E5%AE%9E%E7%94%A8%E8%84%9A%E6%9C%AC/%E8%8E%B7%E5%8F%96%E5%B9%B6%E5%A4%8D%E5%88%B6%E5%BD%93%E5%89%8D%E9%A1%B5%E9%9D%A2Cookies/Cookies-icon.png
// @updateURL https://gitee.com/selling-surprises/Greasyfork-Script-repository/raw/main/%E5%AE%9E%E7%94%A8%E8%84%9A%E6%9C%AC/%E8%8E%B7%E5%8F%96%E5%B9%B6%E5%A4%8D%E5%88%B6%E5%BD%93%E5%89%8D%E9%A1%B5%E9%9D%A2Cookies/%E8%8E%B7%E5%8F%96%E5%B9%B6%E5%A4%8D%E5%88%B6%E5%BD%93%E5%89%8D%E9%A1%B5%E9%9D%A2Cookies.user.js
// ==/UserScript==

(function() {
    'use strict';

    // 配置常量
    const CONFIG = {
        enabled: GM_getValue('cookiesHelperEnabled', true),
        autoCopy: GM_getValue('cookiesHelperAutoCopy', false),
        showNotifications: GM_getValue('cookiesHelperNotifications', true),
        btnText: '获取 Cookies',
        btnPosition: { bottom: '20px', right: '20px' },
        panelPosition: { bottom: '100px', right: '20px' },
        copyBtnPosition: { bottom: '60px', right: '20px' }
    };

    // 创建主按钮
    function createMainButton() {
        // 如果按钮已存在则不再创建
        if (document.getElementById('cookies-helper-btn')) return;

        const button = document.createElement('button');
        button.id = 'cookies-helper-btn';
        button.innerText = CONFIG.btnText;
        button.style.position = 'fixed';
        button.style.bottom = CONFIG.btnPosition.bottom;
        button.style.right = CONFIG.btnPosition.right;
        button.style.zIndex = '99999';
        button.style.padding = '12px 24px';
        button.style.backgroundColor = '#4CAF50';
        button.style.color = 'white';
        button.style.border = 'none';
        button.style.borderRadius = '30px';
        button.style.cursor = 'pointer';
        button.style.fontFamily = 'Arial, sans-serif';
        button.style.fontSize = '16px';
        button.style.fontWeight = 'bold';
        button.style.boxShadow = '0 4px 15px rgba(0,0,0,0.2)';
        button.style.transition = 'all 0.3s';

        button.addEventListener('mouseenter', () => {
            button.style.transform = 'translateY(-3px)';
            button.style.boxShadow = '0 6px 20px rgba(0,0,0,0.3)';
            button.style.backgroundColor = '#45a049';
        });

        button.addEventListener('mouseleave', () => {
            button.style.transform = 'translateY(0)';
            button.style.boxShadow = '0 4px 15px rgba(0,0,0,0.2)';
            button.style.backgroundColor = '#4CAF50';
        });

        button.addEventListener('click', showCookiesPanel);

        document.body.appendChild(button);
    }

    // 显示Cookies面板
    function showCookiesPanel() {
        const cookies = document.cookie;

        // 移除可能存在的旧面板
        removeCookiesPanel();

        // 创建显示框
        const panel = document.createElement('div');
        panel.id = 'cookies-panel';
        panel.style.position = 'fixed';
        panel.style.bottom = CONFIG.panelPosition.bottom;
        panel.style.right = CONFIG.panelPosition.right;
        panel.style.zIndex = '99999';
        panel.style.backgroundColor = '#fff';
        panel.style.border = '1px solid #e0e0e0';
        panel.style.padding = '20px';
        panel.style.width = '350px';
        panel.style.maxHeight = '300px';
        panel.style.overflowY = 'auto';
        panel.style.borderRadius = '12px';
        panel.style.boxShadow = '0 10px 30px rgba(0,0,0,0.15)';
        panel.style.fontFamily = 'Arial, sans-serif';
        panel.style.color = '#333';

        const title = document.createElement('h3');
        title.innerText = '当前页面Cookies';
        title.style.marginTop = '0';
        title.style.marginBottom = '15px';
        title.style.color = '#2c3e50';
        title.style.fontSize = '18px';

        const content = document.createElement('pre');
        content.id = 'cookies-content';
        content.style.whiteSpace = 'pre-wrap';
        content.style.wordBreak = 'break-word';
        content.style.fontSize = '14px';
        content.style.lineHeight = '1.5';
        content.style.marginBottom = '20px';
        content.innerText = cookies || '此网站没有Cookies';

        panel.appendChild(title);
        panel.appendChild(content);

        // 创建操作按钮容器
        const actions = document.createElement('div');
        actions.style.display = 'flex';
        actions.style.gap = '10px';
        actions.style.justifyContent = 'flex-end';

        // 创建复制按钮
        const copyButton = document.createElement('button');
        copyButton.id = 'copy-cookies-btn';
        copyButton.innerHTML = '📋 复制到剪贴板';
        copyButton.disabled = !cookies;
        copyButton.style.padding = '10px 18px';
        copyButton.style.backgroundColor = cookies ? '#2196F3' : '#cccccc';
        copyButton.style.color = 'white';
        copyButton.style.border = 'none';
        copyButton.style.borderRadius = '6px';
        copyButton.style.cursor = cookies ? 'pointer' : 'not-allowed';
        copyButton.style.fontFamily = 'Arial, sans-serif';
        copyButton.style.fontSize = '14px';
        copyButton.style.transition = 'all 0.2s';

        copyButton.addEventListener('click', () => {
            copyCookiesToClipboard(cookies);
        });

        // 创建关闭按钮
        const closeButton = document.createElement('button');
        closeButton.innerHTML = '× 关闭';
        closeButton.style.padding = '10px 18px';
        closeButton.style.backgroundColor = '#e0e0e0';
        closeButton.style.color = '#333';
        closeButton.style.border = 'none';
        closeButton.style.borderRadius = '6px';
        closeButton.style.cursor = 'pointer';
        closeButton.style.fontFamily = 'Arial, sans-serif';
        closeButton.style.fontSize = '14px';
        closeButton.style.transition = 'all 0.2s';

        closeButton.addEventListener('click', () => {
            removeCookiesPanel();
        });

        actions.appendChild(closeButton);
        actions.appendChild(copyButton);
        panel.appendChild(actions);

        document.body.appendChild(panel);

        // 如果启用自动复制，则自动复制到剪贴板
        if (CONFIG.autoCopy && cookies) {
            setTimeout(() => {
                copyCookiesToClipboard(cookies);
            }, 300);
        }
    }

    // 复制Cookies到剪贴板
    function copyCookiesToClipboard(cookies) {
        if (!cookies) return;

        navigator.clipboard.writeText(cookies).then(() => {
            if (CONFIG.showNotifications) {
                showNotification('Cookies已复制到剪贴板!', '#4CAF50');
            }
        }).catch(err => {
            if (CONFIG.showNotifications) {
                showNotification('复制失败: ' + err, '#f44336');
            }
        });
    }

    // 显示通知
    function showNotification(message, color) {
        // 移除旧的通知
        const oldNotification = document.getElementById('cookies-notification');
        if (oldNotification) oldNotification.remove();

        const notification = document.createElement('div');
        notification.id = 'cookies-notification';
        notification.innerText = message;
        notification.style.position = 'fixed';
        notification.style.bottom = '180px';
        notification.style.right = '20px';
        notification.style.zIndex = '99999';
        notification.style.backgroundColor = color;
        notification.style.color = 'white';
        notification.style.padding = '15px 25px';
        notification.style.borderRadius = '8px';
        notification.style.fontFamily = 'Arial, sans-serif';
        notification.style.fontSize = '14px';
        notification.style.boxShadow = '0 5px 15px rgba(0,0,0,0.2)';
        notification.style.opacity = '0';
        notification.style.transform = 'translateY(20px)';
        notification.style.transition = 'all 0.3s ease';

        document.body.appendChild(notification);

        // 显示通知
        setTimeout(() => {
            notification.style.opacity = '1';
            notification.style.transform = 'translateY(0)';
        }, 10);

        // 3秒后移除通知
        setTimeout(() => {
            notification.style.opacity = '0';
            notification.style.transform = 'translateY(20px)';
            setTimeout(() => {
                notification.remove();
            }, 300);
        }, 3000);
    }

    // 移除Cookies面板
    function removeCookiesPanel() {
        const panel = document.getElementById('cookies-panel');
        if (panel) panel.remove();
    }

    // 初始化后台菜单
    function initMenu() {
        // 注册菜单命令
        GM_registerMenuCommand(`脚本状态: ${CONFIG.enabled ? '启用' : '禁用'}`, toggleScript);
        GM_registerMenuCommand(`自动复制: ${CONFIG.autoCopy ? '开启' : '关闭'}`, toggleAutoCopy);
        GM_registerMenuCommand(`通知: ${CONFIG.showNotifications ? '开启' : '关闭'}`, toggleNotifications);
    }

    // 切换脚本状态
    function toggleScript() {
        CONFIG.enabled = !CONFIG.enabled;
        GM_setValue('cookiesHelperEnabled', CONFIG.enabled);

        if (CONFIG.enabled) {
            createMainButton();
            showNotification('Cookies助手已启用!', '#4CAF50');
        } else {
            const button = document.getElementById('cookies-helper-btn');
            if (button) button.remove();
            removeCookiesPanel();
            showNotification('Cookies助手已禁用!', '#f44336');
        }
    }

    // 切换自动复制
    function toggleAutoCopy() {
        CONFIG.autoCopy = !CONFIG.autoCopy;
        GM_setValue('cookiesHelperAutoCopy', CONFIG.autoCopy);
        showNotification(`自动复制已${CONFIG.autoCopy ? '启用' : '禁用'}!`, CONFIG.autoCopy ? '#4CAF50' : '#f44336');
    }

    // 切换通知
    function toggleNotifications() {
        CONFIG.showNotifications = !CONFIG.showNotifications;
        GM_setValue('cookiesHelperNotifications', CONFIG.showNotifications);
        showNotification(`通知已${CONFIG.showNotifications ? '启用' : '禁用'}!`, CONFIG.showNotifications ? '#4CAF50' : '#f44336');
    }

    // 初始化脚本
    if (CONFIG.enabled) {
        // 等待页面加载完成
        if (document.readyState === 'complete') {
            createMainButton();
        } else {
            window.addEventListener('load', createMainButton);
        }
    }

    // 注册后台菜单
    initMenu();
})();